//
//  SetingUpViewController.m
//  werfgh
//
//  Created by 李晓勇 on 14-9-18.
//  Copyright (c) 2014年 dingshenwangluokeji. All rights reserved.
//

#import "SetingUpViewController.h"
#import "LogViewController.h"
#import "XiugViewController.h"
#import "Header.h"
#import "MicroMallViewController.h"
#import "UMFeedback.h"
#import "MyBillledViewController.h"
@interface SetingUpViewController ()<UITableViewDataSource,UITableViewDelegate,UIApplicationDelegate>
{
    NSArray*arry;
    UISwitch*switchView;
}

@end

@implementation SetingUpViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initializationnsa
        // arry=@[@"呼叫等待音乐",@"修改密码",@"常见问题",@"注销账号",@"退出程序"];
        arry=@[@"呼叫等待音乐",@"常见问题",@"我的话单",@"用户反馈",@"注销账号",@"退出程序"];
    }
    return self;
}
-(void)addBack
{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 28, 28);
    
    [backBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
    // Do any additional setup after loading the view.
    
}
-(void)doBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"设置";
    [self addBack];
    UITableView*tabV=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStyleGrouped];
    tabV.delegate=self;
    tabV.dataSource=self;
    [self.view addSubview:tabV];
    // Do any additional setup after loading the view.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (DeviceHeight==480) {
        return 5;
    }
    else
    {
        return 15;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (DeviceHeight==480)
    {
        return 40;
    }
    else
    {
        return 44;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [arry count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text=arry[indexPath.section];
    if (indexPath.section==0) {
        [self setUpSwitchView];
        [cell addSubview:switchView];
        cell.contentView.backgroundColor = [UIColor clearColor];
        UIView *aView = [[UIView alloc] initWithFrame:cell.contentView.frame];
        aView.backgroundColor = [UIColor clearColor];
        cell.selectedBackgroundView = aView;
        
    }
    else
    {
        
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        
    }
    return cell;
}
-(void)setUpSwitchView
{
    if (IOS5) {
        switchView=[[UISwitch alloc]initWithFrame:CGRectMake(220, 8, 0, 0)];
    }
    else
    {
        switchView=[[UISwitch alloc]initWithFrame:CGRectMake(250, 8, 0, 0)];
    }
    BOOL switchIsOn=YES;
    switchIsOn=[[NSUserDefaults standardUserDefaults]boolForKey:@"SWITCH"];
   
    switchView.on=switchIsOn;

    //switchView.onTintColor=[UIColor blueColor];
    [switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
}
-(void)switchAction:sender
{
    UISwitch * tmpSwitch = (UISwitch *)sender;
    //强制转换sender的类型，sender代表发送者
    
    [[NSUserDefaults standardUserDefaults]setBool:tmpSwitch.on forKey:@"SWITCH"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSNumber *selRow = [[NSNumber alloc] initWithInteger:indexPath.section];
    int i=[selRow intValue];
    switch (i ) {
        case 0:
        {
            return;
        }
            break;
        case 1:
        {
            [self.navigationController pushViewController:[MicroMallViewController alloc] animated:YES];
        }
            break;
        case 2:
           [self.navigationController pushViewController:[MyBillledViewController alloc] animated:YES];
            break;
        case 3:
        {
            [self presentModalViewController:[UMFeedback feedbackModalViewController]
                                    animated:YES];        }
            break;
            
        case 4:
            [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"newusers1"];
            [[NSUserDefaults standardUserDefaults]synchronize];
            [self presentViewController:[LogViewController
                                         alloc] animated:YES completion:^{
                //[self .view removeFromSuperview];
            }];
            
            break;
        case 5:
            [self exitApplication ];
            break;
            
        default:
            break;
    }
}




- (void)exitApplication {
    
    // [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"newusers1"];
    NSLog(@"1111%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"newusers1"]);
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"newusers1"];
    NSLog(@"1111%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"newusers1"]);
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    
    [UIView animateWithDuration:1.0f animations:^{
        window.alpha = 0;
        window.frame = CGRectMake(window.bounds.size.width/2, window.bounds.size.height/2, 0, 0);
    } completion:^(BOOL finished) {
        exit(0);
    }];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
